/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package indexer;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.HashSet;
import org.omg.CORBA.INTERNAL;

/**
 *
 * @author Claudiu
 */
 class BuildReverseIndex {
     //should build index files for given list of files...
   private ArrayList<String> listOfTextFiles;
    private ArrayList<String> listOfIndexFiles;
    private static  String documentEncodingFileName = " documentList.txt";

    private String excludeTextFile;
    private String inverseIndexTextFile;
    public BuildReverseIndex(ArrayList<String> _listTextFiles)
    {
       listOfTextFiles = _listTextFiles;
       listOfIndexFiles = new ArrayList<String>();
       inverseIndexTextFile = " inverserIndex.txt";

    }
    void BuildDocumentListFile()
    {
        try{
            FileWriter writer = new FileWriter(documentEncodingFileName);
            BufferedWriter file = new BufferedWriter(writer);

            for ( int i=0 ; i <getListOfTextFiles().size(); i++)
            {
                file.append(""+i+ " : "+getListOfTextFiles().get(i).replaceAll("text", "doc ") + "\n");

            }
            file.close();
            writer.close();

        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
    public static ArrayList<String > GetFileNames (int [] p)
    {
        HashSet<Integer> hashSet = new HashSet<Integer>();
        
        for( int i=0 ; i < p.length; i++)
        {
            hashSet.add(i);
        }
       ArrayList <String> documentList= new ArrayList<String>();
       try {
            FileReader reader = new FileReader(documentEncodingFileName);
            BufferedReader input = new BufferedReader(reader);
            String line = input.readLine();
            while (line != null) {
                String[] split = new String[30];
               
                try{
                     split = line.split(" : ");
                     Integer id= Integer.parseInt( split[0].trim());
                     if(hashSet.contains(id))
                     {
                         documentList.add(split[1]);
                     }
                    
                } catch (Exception e)
                {
                               System.out.println(e.getMessage());
           e.printStackTrace();
                }
            line= input.readLine();
            }
       }catch(Exception e)
       {
           System.out.println(e.getMessage());
           e.printStackTrace();
       }
       return documentList;
        
    }
    public void MakeIndexFiles()
    {
        setExcludeTextFile("excludeWords.txt");
      ReverseIndex  rev= new ReverseIndex();
       for( int i =0; i < getListOfTextFiles().size() ; i++)
       {
           String indexFileName = getListOfTextFiles().get(i).replaceAll("text", "index");
           Parser p= new Parser(getListOfTextFiles().get(i), indexFileName, i);
           p.GetExcludeWordsFromFile(getExcludeTextFile());
           p.ParseFile();
           rev.parseDirectIndexFile(indexFileName, i);
       }
      rev.saveReverseIndexToFile(getInverseIndexTextFile());
      BuildDocumentListFile();
    }

    /**
     * @return the listOfTextFiles
     */
    public ArrayList<String> getListOfTextFiles() {
        return listOfTextFiles;
    }

    /**
     * @param listOfTextFiles the listOfTextFiles to set
     */
    public void setListOfTextFiles(ArrayList<String> listOfTextFiles) {
        this.listOfTextFiles = listOfTextFiles;
    }

    /**
     * @return the listOfIndexFiles
     */
    public ArrayList<String> getListOfIndexFiles() {
        return listOfIndexFiles;
    }

    /**
     * @param listOfIndexFiles the listOfIndexFiles to set
     */
    public void setListOfIndexFiles(ArrayList<String> listOfIndexFiles) {
        this.listOfIndexFiles = listOfIndexFiles;
    }

    /**
     * @return the excludeTextFile
     */
    public String getExcludeTextFile() {
        return excludeTextFile;
    }

    /**
     * @param excludeTextFile the excludeTextFile to set
     */
    public void setExcludeTextFile(String excludeTextFile) {
        this.excludeTextFile = excludeTextFile;
    }

    /**
     * @return the inverseIndexTextFile
     */
    public String getInverseIndexTextFile() {
        return inverseIndexTextFile;
    }

    /**
     * @param inverseIndexTextFile the inverseIndexTextFile to set
     */
    public void setInverseIndexTextFile(String inverseIndexTextFile) {
        this.inverseIndexTextFile = inverseIndexTextFile;
    }





    // Arra
}
